package cn.dnui_yxk801.dnuifood_16110100801.controller;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import cn.dnui_yxk801.dnuifood_16110100801.R;


/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment2 extends Fragment {
    private TextView collect_shop,collect_food;
    private CollectFoodFragment collectFoodFragment;
    private CollectShopFragment collectShopFragment;
    private View view=null;
    protected FragmentManager childManager;
    private FragmentTransaction transaction;


    public Fragment2() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
//        Fragment childFragment=new CollectFoodFragment();
//        FragmentTransaction transaction=getChildFragmentManager().beginTransaction();
//        transaction.add(R.id.fragment_container,childFragment);
//        return inflater.inflate(R.layout.fragment2, container, false);
        view=inflater.inflate(R.layout.fragment2,container,false);
        initViews();
        initEvents();
        return view;
    }
    public void onResume(){
        super.onResume();
        if (collectShopFragment==null){
            collectShopFragment=new CollectShopFragment();
            addFragment(collectShopFragment);
        }else {
            //replaceFragment(collectShopFragment);
        }
    }
    private void initViews(){
        childManager=getChildFragmentManager();
        collect_shop=(TextView)view.findViewById(R.id.ct_tv_shop);
        collect_food=(TextView)view.findViewById(R.id.ct_tv_food);
        //collect_shop.setSelected(true);
        collect_shop.setTextColor(getResources().getColor(R.color.colorPrimary));
        collect_food.setTextColor(getResources().getColor(R.color.yinse));
    }
    private void initEvents(){
        collect_shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (collectShopFragment==null){
                    collectShopFragment=new CollectShopFragment();
                }
                replaceFragment(collectShopFragment);

                //collect_shop.setSelected(true);
                //collect_food.setSelected(false);
                collect_shop.setTextColor(getResources().getColor(R.color.colorPrimary));
                collect_food.setTextColor(getResources().getColor(R.color.yinse));
            }
        });
        collect_food.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (collectFoodFragment==null){
                    collectFoodFragment=new CollectFoodFragment();
                }
                replaceFragment(collectFoodFragment);

                //collect_shop.setSelected(false);
                //collect_food.setSelected(true);
                collect_shop.setTextColor(getResources().getColor(R.color.yinse));
                collect_food.setTextColor(getResources().getColor(R.color.colorPrimary));
            }
        });
    }
    protected void addFragment(Fragment fragment){
        transaction=childManager.beginTransaction();
        transaction.add(R.id.fragment_container,fragment);
        transaction.commit();
    }
    public  void onAttach(Context context){
        super.onAttach(context);
    }

    protected void replaceFragment(Fragment fragment){
        transaction=childManager.beginTransaction();
        transaction.replace(R.id.fragment_container,fragment);
        transaction.commit();
    }

}
